package org.wlgzs.office_exam_student.servlet;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.wlgzs.office_exam_student.entity.ChoiceQuestion;
import org.wlgzs.office_exam_student.entity.OperateQuestion;
import org.wlgzs.office_exam_student.entity.User;
import org.wlgzs.office_exam_student.permission.Authentication;
import org.wlgzs.office_exam_student.service.office.OfficeConfig;
import org.wlgzs.office_exam_student.service.office.ResolveTask;

/**
 * @author 孤城落寞的博客
 *
 *         专项练习的界面
 */
@WebServlet("/nicetyPractice")
public class NicetyPracticeServlet extends BaseServlet {
	//
	private static final long serialVersionUID = 1L;
	/**
	 * 专项练习的需要出的试题数 初始化参数为 选择 ： 40 word：2 excel：2 ppt：2
	 *
	 */
	private final int choiceNumber = 3;
	private final int operateNumber = 2;
	
	private static final String resolveTasksKey = "resolveTasks";
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		List defQuestions = new LinkedList<>();
		config.getServletContext().setAttribute("choiceList", defQuestions);
		config.getServletContext().setAttribute("word", defQuestions);
		config.getServletContext().setAttribute("excel", defQuestions);
		config.getServletContext().setAttribute("ppt", defQuestions);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// 需要的参数 ： 选择的题型 type 和 考试等级 paper
		// 模拟参数
		String paperStr = req.getParameter("paper");
		int paper = 1;
		try {
			paper = Integer.parseInt(paperStr);
		}catch(Exception e) {}
		String type = req.getParameter("type");
		HttpSession ses = req.getSession();
		ses.setAttribute("action", "mock");
		User user = Authentication.user();

		String level = paper == 1 ? ChoiceQuestion.GRADE_TWO : ChoiceQuestion.GRADE_TWO;
		
		if ("choice".equals(type)) {
			req.setAttribute("choiceList",
					choiceQuestionService.choiceQuestions(user.getSchoolId(), choiceNumber, level));
		} else {
			
			//解析任务
			List<ResolveTask> resolveTasks = new LinkedList<>();
			//先存进去，下面对列表进行添加
			ses.setAttribute(resolveTasksKey, resolveTasks);
			if (OperateQuestion.TYPE_WORD.equals(type) || OperateQuestion.TYPE_PPT.equals(type)
					|| OperateQuestion.TYPE_EXCEL.equals(type)) {
				List<OperateQuestion> questions = operateQuestionService.operateQuestions(user.getSchoolId(), operateNumber, type, level);
				if(questions != null) {
					req.setAttribute(type,questions);
					int index = 0;
					for(OperateQuestion oq:questions) {
						OfficeConfig.FileType fileType = null;
						if(OperateQuestion.TYPE_WORD.equals(type)) {
							fileType = OfficeConfig.FileType.DOCX;
						}else if(OperateQuestion.TYPE_EXCEL.equals(type)) {
							fileType = OfficeConfig.FileType.XLSX;
						}else if(OperateQuestion.TYPE_PPT.equals(type)) {
							fileType = OfficeConfig.FileType.PPTX;
						}
						ResolveTask task = new ResolveTask(user.getId(), String.valueOf(oq.getId()), fileType, OfficeConfig.PaperType.MOCK);
						task.setIndex(index++);
			        	resolveTasks.add(task);
					}
				}
			}
		}
		render("nicetyPractice");
	}
}
